package com.csii.galaxy.studio.rest;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.csii.core.api.Result;
import com.csii.core.system.vo.SyncDictVo;
import com.csii.galaxy.studio.entity.ActStartFlowInstance;
import com.csii.galaxy.studio.entity.ActStartFlowInstanceIframeUrl;
import com.csii.galaxy.studio.entity.SendMessage;
import com.csii.galaxy.studio.service.GalaxyStudioBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2020/11/2 17:30
 * @Created by Ian
 * @Contact: 19107614@qq.com
 */
@Service
public class GalaxuStudioServiceRest  implements GalaxyStudioBaseService {
    @Value("${galaxy.studio.url}")
    private String galaxyStudioPrefix="";

    @Autowired
    RestTemplate restTemplate;

    @Override
    public String queryTableDictTextByKey(String table, String text, String code, String key) {
        Map<String,Object> map = new HashMap<>();
        map.put("table",table);
        map.put("text",text);
        map.put("code",code);
        map.put("key",key);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
       return HttpUtil.createGet(String.format(galaxyStudioPrefix+"/sys/dict/queryTableDictTextByKey?table=%s&text=%s&code=%s&key=%s",table,text,code,key ))
               .header("X-Access-Token",attributes.getRequest().getHeader("X-Access-Token")).execute().body();
    }

    @Override
    public String queryDictTextByKey(String code, String key) {
        Map<String,Object> map = new HashMap<>();
        map.put("code",code);
        map.put("key",key);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return HttpUtil.createGet(String.format(galaxyStudioPrefix+"/sys/dict/queryDictTextByKey?code=%s&key=%s",code,key ))
                .header("X-Access-Token",attributes.getRequest().getHeader("X-Access-Token")).execute().body();
    }

    @Override
    public String parseTemplateByCode(String templateCode, Map<String, String> map) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return HttpUtil.createPost(galaxyStudioPrefix+"/sys/common/parseTemplateByCode/"+templateCode).body(JSONObject.toJSONString(map))
                .header("X-Access-Token",attributes.getRequest().getHeader("X-Access-Token")).execute().body();
    }

    @Override
    public Result sendSysAnnouncement(SendMessage sendMessage) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<SendMessage> entity = new HttpEntity<>(sendMessage, headers);
        return restTemplate.exchange(galaxyStudioPrefix + "/sys/annountCement/sendSysAnnouncement/api", HttpMethod.POST, entity, new ParameterizedTypeReference<Result>() {}).getBody();
    }

    @Override
    public Result sendSysAnnouncementAct(String actBusId, String fromUser, String toUser, String title, String msgContent) {
        Map<String,Object> map = new HashMap<>();
        map.put("actBusId",actBusId);
        map.put("fromUser",fromUser);
        map.put("toUser",toUser);
        map.put("title",title);
        map.put("msgContent",msgContent);
        return restTemplate.getForObject(galaxyStudioPrefix+"/sys/annountCement/sendSysAnnouncementAct?actBusId={actBusId}&fromUser={fromUser}&toUser={toUser}&title={title}&msgContent={msgContent}",Result.class,map);
    }

    @Override
    public SyncDictVo syncDict(String redisListKey,Integer lastSyncResult) {
        return restTemplate.exchange(galaxyStudioPrefix + "/sys/dict/syncDict/"+redisListKey+"/"+lastSyncResult, HttpMethod.GET, null, new ParameterizedTypeReference<SyncDictVo>() {}).getBody();
    }

    @Override
    public Result<List<String>> queryClientIdList() {
        return restTemplate.exchange(galaxyStudioPrefix + "/oauthClientDetails/queryClientIdList", HttpMethod.GET, null, new ParameterizedTypeReference<Result<List<String>>>() {}).getBody();

    }

    @Override
    public Result pay(String appCode, int count) {
        Map<String,Object> map = new HashMap<>();
        map.put("appCode",appCode);
        map.put("count",count);
        return restTemplate.getForObject(galaxyStudioPrefix + "/sysSaasAppInfo/pay?appCode={appCode}&count={count}",Result.class,map);

    }

    @Override
    public Result costAuth(String appCode) {
        Map<String,Object> map = new HashMap<>();
        map.put("appCode",appCode);
        return restTemplate.getForObject(galaxyStudioPrefix + "/sysSaasAppInfo/costAuth?appCode={appCode}", Result.class,map);
    }

    @Override
    public Result startFlowInstanceByKey(ActStartFlowInstance startFlowInstance) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<ActStartFlowInstance> entity = new HttpEntity<>(startFlowInstance, headers);
        return restTemplate.exchange(galaxyStudioPrefix + "/activiti/flow/operation/startFlowInstanceIframeUrlByKey", HttpMethod.POST, entity, new ParameterizedTypeReference<Result>() {}).getBody();
    }

    @Override
    public Result startFlowInstanceIframeUrlByKey(ActStartFlowInstanceIframeUrl startFlowInstanceIframeUrl) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<ActStartFlowInstanceIframeUrl> entity = new HttpEntity<>(startFlowInstanceIframeUrl, headers);
        return restTemplate.exchange(galaxyStudioPrefix + "/activiti/flow/operation/startFlowInstanceIframeUrlByKey", HttpMethod.POST, entity, new ParameterizedTypeReference<Result>() {}).getBody();
    }
}
